package enterprise.manage.dao;

import enterprise.manage.entity.ProductType;
import enterprise.manage.entity.Products;
import org.hibernate.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.orm.hibernate.HibernateDao;

import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User:zhenmingyue
 * Date: 2012-11-20
 * Desc:
 */
@Repository
@Transactional
public class ProductTypeDao extends HibernateDao<ProductType, Long> {

    public List getProductTypeSelect() {
        return getSession().createQuery("select type from ProductType group by type").list();
    }

    public List getProductBrandSelect(String type) {
        Query query = getSession().createQuery("select brand from ProductType where type =:type group by brand");
        query.setString("type", type);
        return query.list();
    }

    public String getProductBrandSelectOption(String type) {
        List list = getProductBrandSelect(type);
        return getOptionStr(list);
    }
    public String getProductModelSelectOption(String type,String brand) {
        List list = getProductModelSelect(type,brand);
        return getOptionStr(list);
    }
    public List getProductModelSelect(String type, String brand) {
        Query query = getSession().createQuery("select model from ProductType where type =:type and brand =:brand group by model");
        query.setString("type", type);
        query.setString("brand", brand);
        return query.list();
    }

    public ProductType getProductBy(String type, String brand, String model) {
        Query query = getSession().createQuery("from ProductType where type =:type and brand =:brand and model =:model ");
        query.setString("type", type);
        query.setString("brand", brand);
        query.setString("model", model);
        List list = query.list();
        if (list != null && list.size() > 0) {
            return (ProductType) list.get(0);
        }
        return null;
    }

    public String getOptionStr(List list) {
        StringBuffer stringBuffer = new StringBuffer();
        for (Object s : list) {
            stringBuffer.append("<option value='").append(s).append("'>").append(s).append("</option>");
        }
        return stringBuffer.toString();
    }
}